/*
* BlowerCalController.java
* Created on  2015/7/27 12:19
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.blower;

import com.infitecs.eyas.blower.model.BlowerBaseCal;
import com.infitecs.eyas.blower.model.BlowerCalculator;
import com.infitecs.eyas.blower.service.BlowerCalculatorService;
import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.oncefan.model.OnceFanCalculator;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/blower/calculate")
public class BlowerCalController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private BlowerCalculatorService blowerCalculatorService;

    @Autowired
    private ToolExpressionService toolExpressionService;

    @RequestMapping("")
    public String getPage(Model model) {
        try {
            sessionService.getCaseId();
            List<BlowerCalculator> blowerCalculators = blowerCalculatorService.getBlowerCalculatorList();
            if (blowerCalculators == null || blowerCalculators.isEmpty()) {
                blowerCalculatorService.insertBlowerCalculator();
            }
            BlowerBaseCal blowerBaseCal = blowerCalculatorService.getBlowerBaseCal();
            if (blowerBaseCal == null) {
                blowerCalculatorService.insertNewOnceFanBase();
                blowerBaseCal = blowerCalculatorService.getBlowerBaseCal();
            }
            List<BlowerCalculator> in = blowerCalculatorService.getIn();
            List<BlowerCalculator> out = blowerCalculatorService.getOut();
            model.addAttribute("in", in);
            model.addAttribute("out", out);
            model.addAttribute("blowerBaseCal", blowerBaseCal);
            sessionService.setEnvironment('S');
        } catch (Exception e) {
            return "blower/calculate";
        }
        return "blower/calculate";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int add(BlowerCalculator blowerCalculator) {
        return blowerCalculatorService.insertBlower(blowerCalculator);
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public void del(@RequestParam("id") int id) {
        blowerCalculatorService.del(id);
    }

    @RequestMapping(value = "/editTitle", method = RequestMethod.POST)
    @ResponseBody
    public void editTitle(BlowerCalculator blowerCalculator) {
        blowerCalculatorService.editTitle(blowerCalculator);
    }

    @RequestMapping(value = "/setE", method = RequestMethod.GET)
    @ResponseBody
    public void setEv() {
        sessionService.setEnvironment('S');
    }

    @RequestMapping(value = "/getToolBarValues", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Double> getToolBarValues(@RequestParam("id") int id) {
        sessionService.setEnvironment('S');
        return toolExpressionService.getResult();
    }

    @RequestMapping(value = "/autoCal", method = RequestMethod.POST)
    @ResponseBody
    public Double autoCal(BlowerCalculator blowerCalculator) {
        return blowerCalculator.getP() * blowerCalculator.getZ();
    }

    @RequestMapping(value = "/autoCalSum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> autoCalSum(@RequestBody List<BlowerCalculator> blowerCalculators) {
        //E17获取
        Double E17 = sessionService.getParamMap().get("SE14");
        double calculateLost = 0;
        double designLost = 0;
        double t;
        Iterator<BlowerCalculator> i = blowerCalculators.iterator();
        BlowerCalculator b;
        while (i.hasNext()) {
            b = i.next();
            t = b.getLost();
            calculateLost += t;
            if (b.isCheck2()) {
                if (E17 != null) {
                    t *= (1 + E17 / 100);
                }else {
                    return null;
                }
            }
            designLost += t;
        }
        Map<String, String> result = new HashMap<>();
        result.put("calculateLost", Double.toString(calculateLost));
        result.put("designLost", Double.toString(designLost));
        this.updateBlowerRowSum(blowerCalculators.get(0), calculateLost, designLost);
        return result;
    }

    @RequestMapping(value = "/saveRow", method = RequestMethod.POST)
    @ResponseBody
    public void saveRow(BlowerCalculator blowerCalculator) {
        blowerCalculatorService.update(blowerCalculator);
    }

    @RequestMapping(value = "/autoCalTableTotal", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> updateTotal(@RequestBody Map<String, Object> wrapper) {
        Double E25 = sessionService.getParamMap().get("SE25");
        Double E20 = sessionService.getParamMap().get("SE20");
        if (E25 == null || E20 == null){
            return null;
        }
        int source = Integer.parseInt(wrapper.get("source").toString());
        List<Map<String, String>> dataWrapper = (List<Map<String, String>>) wrapper.get("data");
        double calculateLostTotal = 0;
        double designLostTotal = 0;
        Map<String, String> data;
        Iterator<Map<String, String>> i = dataWrapper.iterator();
        while (i.hasNext()) {
            data = i.next();
            calculateLostTotal += Double.parseDouble(data.get("calculateLost"));
            designLostTotal += Double.parseDouble(data.get("designLost"));
        }
        Map<String, String> result = new HashMap<>();


        if (1 == source) {
            result.put("E27", Double.toString(calculateLostTotal + E20));
            this.updateBlowerCalculator("E27", calculateLostTotal + E20);
            result.put("E30", Double.toString(designLostTotal + E20));
            this.updateBlowerCalculator("E30", designLostTotal + E20);
        } else {
            result.put("E28", Double.toString(calculateLostTotal + E25));
            this.updateBlowerCalculator("E28", calculateLostTotal + E25);
            result.put("E31", Double.toString(designLostTotal + E25));
            this.updateBlowerCalculator("E31", designLostTotal + E25);
        }
        return result;
    }

    @RequestMapping(value = "/updateParam", method = RequestMethod.POST)
    @ResponseBody
    public void updateBlowerCalculator(@RequestParam String paramName, @RequestParam Double paramValue) {
        BlowerBaseCal BlowerBaseCal = new BlowerBaseCal();
        BlowerBaseCal.set(paramName, paramValue);
        BlowerBaseCal.setCaseId(sessionService.getCaseId());
        sessionService.putParamMap(new Param(sessionService.getE() + paramName,paramValue));
        blowerCalculatorService.updateParam(BlowerBaseCal);
    }

    private void updateBlowerRowSum(BlowerCalculator blowerCalculator, double RowCalculateLostSum, double RowDesignLostSum) {
        StringBuilder paramName = new StringBuilder();
        switch (blowerCalculator.getSmallType()) {
            case 1:
                paramName.append("pipe_");
                break;
            case 2:
                paramName.append("parts_");
                break;
            case 3:
                paramName.append("spare_");
                break;
            case 4:
                paramName.append("device_");
                break;
            case 5:
                paramName.append("other_");
                break;
            default:
                break;
        }
        if (blowerCalculator.getLagreType() == 1) {
            paramName.append("in_");
        } else {
            paramName.append("out_");
        }
        this.updateBlowerCalculator(paramName.toString() + "one", RowCalculateLostSum);
        this.updateBlowerCalculator(paramName.toString() + "two", RowDesignLostSum);
    }

    @RequestMapping(value = "/getCalculateInit",method = RequestMethod.POST)
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putBlowerCalInit(sessionService.getCaseId());
    }
}
